package com.huawei.chenxuan;

import lombok.extern.slf4j.Slf4j;
import com.huawei.chenxuan.Computer;
import com.huawei.chenxuan.Judge;
import com.huawei.chenxuan.JudgeIMPH;
import com.huawei.chenxuan.Person;
import com.huawei.chenxuan.ResultFile;

import java.util.*;
import java.util.concurrent.Callable;

/**
 * @program:IntelliJ IDEA
 * @Description:继承线程类，创造子线程
 * @author:Mr.CheCheHuiBujianO
 * @data: 2022/5/4 12:02
 */
@Slf4j
public class GameThread implements Callable<String> {
    @Override
    public String call() {
        String result = null;
        Judge judge = new JudgeIMPH();
        Scanner in = new Scanner(System.in);
        //创建人对象，然后得到手势
        Person person = new Person("","");
        //创建电脑对象，然后得到手势
        Computer computer = new Computer("","");
        Random random = new Random();
        //创建file来存储数据
        ResultFile resultFile = new ResultFile();
        Map<String,String> gameResult = new HashMap<>();
        //进行判断语句
        int count = 1,win = 0,debeat = 0;
        while(count <= 5 && win <=3 && debeat <= 3){
            int input = -1;
            //使用person1.guesture接人输入
            while(true) {
                try {
                    log.info("请输入你要选择的手势（0，石头，1，剪刀，2布）：");
                    input = in.nextInt();
                    person.guesture = person.getGuesture(input);

                    if (person.guesture <= 2 && person.guesture >= 0) {
                        break;
                    }
                } catch (Exception e){
                    log.info("请检查你的输入，请再次输入！！！");
                    in.next();
                }
            }
            //使用end来接judge方法的返回值
            int computerGuesture = random.nextInt(3);
            result = judge.judge(computerGuesture , person.guesture);
            if (result.equals("胜利"))win++;
            else if (result.equals("失败"))debeat++;
            log.info("这局游戏的结果是"+ result);
            log.info("现在游戏已经进行"+count+",其中胜利了"+win+"，失败了"+debeat);
            gameResult.put("第"+count+"局",result);
            count++;
        }
        resultFile.addDate(gameResult);
        while(true){
            System.out.println("请输入你需要查找第几局的结果！");
            System.out.println("输入-1退出程序");
            Scanner scanner = new Scanner(System.in);
            int choose = scanner.nextInt();
            try {
                if (choose == -1)break;
                System.out.println(gameResult.get("第" + choose + "局"));
            } catch (Exception e) {
                System.out.println("请检查你输入局数是否存在！！！");
                choose = scanner.nextInt();
            }
        }
        return result;
    }
}
